package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.AffiliatedFCarEntity;
import com.sgb.kjwl.model.entity.transport.AffiliatedFDetailsListEntity;
import com.sgb.kjwl.model.entity.transport.AffiliatedFDriverEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.GoodsManageEvent;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: 熊丽
 * @date: 2020/12/28 11:10
 * @Description: 车队管理详情model层
 */
public class AffiliatedFDetailsModel extends AndroidViewModel {
    public MutableLiveData<AffiliatedFDetailsListEntity> ldGoods = new MutableLiveData<>();
    public MutableLiveData<List<AffiliatedFCarEntity>> ldCarsList = new MutableLiveData<>();
    public MutableLiveData<List<AffiliatedFDriverEntity>> ldDriverList = new MutableLiveData<>();
    public MutableLiveData<String> lddelete = new MutableLiveData<>();
    public MutableLiveData<String> ldunbind = new MutableLiveData<>();

    public AffiliatedFDetailsModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取外层数据
     */
    public void getAffiliatedFDetails(Context context,String motorcadeNo,int type) {
        DialogHelper.showProgressDialog(context, "加载中", 0, false);
        NewAPIManager.getInstance().getLogisticsAPI().getSelfMotorcadeDetail(10,motorcadeNo,type).enqueue(new ICallBack<BaseEntity<AffiliatedFDetailsListEntity>>() {
            @Override
            public void onSuccess(BaseEntity<AffiliatedFDetailsListEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (!data.isSuccess()) {
                    ldGoods.postValue(new AffiliatedFDetailsListEntity());
                    return;
                }
                ldGoods.postValue(data.getData());
            }
            @Override
            public void onFail() {
                super.onFail();
                ldGoods.postValue(null);
            }
        });
    }

    /**
     * 企业自营和挂靠车队 车辆分页查询
     * @param context
     * @param motorcadeNo
     * @param type
     */
    public void getCarList(Context context,String motorcadeNo,int pageNumber,int type) {
        DialogHelper.showProgressDialog(context, "加载中", 0, false);
        NewAPIManager.getInstance().getLogisticsAPI().getCarList(10,motorcadeNo,pageNumber,type).enqueue(new Callback<BaseEntity<ListResponse<List<AffiliatedFCarEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<AffiliatedFCarEntity>>>> call, Response<BaseEntity<ListResponse<List<AffiliatedFCarEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (!response.isSuccessful()) {
                    ldCarsList.postValue(new ArrayList<>());
                    return;
                }
                ldCarsList.postValue(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<AffiliatedFCarEntity>>>> call, Throwable t) {
                ldCarsList.postValue(null);
            }
        });
    }

    /**
     *
     * @param context
     * @param motorcadeNo
     * @param type
     */
    public void getDriverList(Context context,String motorcadeNo,int pageNumber,int type) {
        DialogHelper.showProgressDialog(context, "加载中", 0, false);
        NewAPIManager.getInstance().getLogisticsAPI().getDriverList(10,motorcadeNo,pageNumber,type).enqueue(new Callback<BaseEntity<ListResponse<List<AffiliatedFDriverEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<AffiliatedFDriverEntity>>>> call, Response<BaseEntity<ListResponse<List<AffiliatedFDriverEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (!response.isSuccessful()) {
                    ldDriverList.postValue(new ArrayList<>());
                    return;
                }
                ldDriverList.postValue(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<AffiliatedFDriverEntity>>>> call, Throwable t) {
                ldCarsList.postValue(null);
            }
        });
    }
    /**
     * 删除
     */
    public void deleteNo(Context context, String no) {
        DialogHelper.showProgressDialog(context, "请稍等...");
        NewAPIManager.getInstance().getLogisticsAPI().deleteProDriver(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if ("200".equals(data.getCode())) {
                    lddelete.postValue("sucess");
                    EventBus.getDefault().post(GoodsManageEvent.make().setMessageTag(EventConstant.EVENT_REFRESH_AFFILIATED_SUCCESS));
                } else {
                    MToast.showToast(getApplication(), "删除失败");
                }
            }
        });
    }
    /**
     * 解除
     */
    public void relation(Context context, String no) {
        DialogHelper.showProgressDialog(context, "请稍等...");
        NewAPIManager.getInstance().getLogisticsAPI().unbindProDriver(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if ("200".equals(data.getCode())) {
                    ldunbind.postValue("sucess");
                    EventBus.getDefault().post(GoodsManageEvent.make().setMessageTag(EventConstant.EVENT_REFRESH_AFFILIATED_SUCCESS));
                } else {
                    MToast.showToast(getApplication(), "解除挂靠失败");
                }
            }
        });
    }
}
